package com.yenlien.traceyun.business.service;


import com.yenlien.traceyun.business.dao.ClientDao;
import com.yenlien.traceyun.business.entity.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/6/8.
 */
@Service
public class ClientService {
    @Autowired
    private ClientDao clientDao;

    public Page<Client> findByList(final Client client) {
        Pageable pageable = PageRequest.of(client.getPage(), client.getPageSize(), Sort.Direction.DESC, "createtime");
        return clientDao.findAll(new Specification<Client>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<Client> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> companyId = root.get("companyId");
                //构造查询条件
                List<Predicate> list = new ArrayList<>();
                List<Predicate> or = new ArrayList<>();
                if (!StringUtils.isEmpty(client.getCompanyId())) {
                    list.add(cb.equal(companyId, client.getCompanyId()));
                }
                Predicate[] predicate = new Predicate[list.size()];
                Predicate pre_and = cb.and(list.toArray(predicate));
                if (!StringUtils.isEmpty(client.getQuery())) {
                    or.add(cb.like(root.get("clientName"), "%" + client.getQuery() + "%"));
                    or.add(cb.like(root.get("userName"), "%" + client.getQuery() + "%"));
                    or.add(cb.like(root.get("phone"), "%" + client.getQuery() + "%"));
                } else {
                    return pre_and;
                }
                Predicate[] pre_or = new Predicate[or.size()];
                Predicate ora = cb.or(or.toArray(pre_or));
                return query.where(pre_and, ora).getRestriction();
            }
        }, pageable);
    }

    public void save(Client client) {
        clientDao.save(client);
    }

    public void update(Client client) {
        clientDao.save(client);
    }

    public Client findById(String id) {
        return clientDao.findById(id);
    }

    public void delete(String id) {
        clientDao.deleteById(id);
    }

    public List<Client> findAll(String companyId) {
        if (!StringUtils.isEmpty(companyId)) {
            return clientDao.findByCompanyId(companyId);
        }
        return clientDao.findAll();
    }

}
